package addressBook;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author Jarvan
 * @version 1.0
 * @create 2020/11/12 20:20
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Contact implements Comparable<Contact>{
    private String name;
    private Address address;
    private String phone;

    /**
     * Use dictionary sort to compare each other.return 1 if this.name > contact.name;
     * (and smaller length is a advantage)
     *
     * @param contact contact object.
     * @return 1 if this.name > contact.name;
     */
    @Override
    public int compareTo(Contact contact) {
        if (contact != null){
            int minLength = this.name.length();
            if (minLength > contact.name.length()){
                minLength = contact.name.length();
            }
            for (int i = 0; i < minLength; i++) {
                if (this.name.charAt(i) > contact.name.charAt(i)){
                    return 1;
                }else if (this.name.charAt(i) < contact.name.charAt(i)) {
                    return -1;
                }
            }//min length is advance.
            if (minLength < contact.name.length()){
                return 1;
            }else if (minLength > contact.name.length()){
                return -1;
            }
        }

        return 0;
    }
}
